import axios from 'axios'
import { safe } from '.'
import { JobRecordModel } from '~/model/JobRecordModel'
import { JobRecordSerializer } from '~/serializer/JobRecordSerializer'

// 用户相关接口
export abstract class JobRecordApi {
  public static async getAll(): Promise<JobRecordModel[]> {
    const res = await axios.get('/job_records/')
    const arr = await res.data

    return Promise.all(
      arr.map((x: any) => JobRecordSerializer.deserialize(x)),
    )
  }

  public static async add(item: JobRecordModel) {
    return safe(() => axios.post(
      'job_records/', JobRecordSerializer.serialize(item),
    ))
  }

  public static async remove(id: number) {
    return safe(() => axios.delete(
      `job_records/${id}/`,
    ))
  }

  public static async update(id: number, item: JobRecordModel) {
    return safe(() => axios.put(
      `job_records/${id}/`,
      JobRecordSerializer.serialize(item),
    ))
  }
}
